﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System.Windows.Controls;
using System.Windows;

namespace RazorSourceGenerator.ViewModel
{
    public class LogViewModel : ToolViewModel
    {
        private string _executingLog;
        public string ExecutingLog
        {
            get
            {
                return _executingLog;
            }
            set
            {
                _executingLog = value;
                this.RaisePropertyChanged("ExecutingLog");
            }
        }

        public LogViewModel() : base("LogView")
        {
            Generator.LogReport += (msg) =>
            {
                _executingLog += msg + System.Environment.NewLine;
                this.RaisePropertyChanged("ExecutingLog");
            };
        }

        private RelayCommand<TextChangedEventArgs> _logChangedCommand;
        public RelayCommand<TextChangedEventArgs> LogChangedCommand
        {
            get
            {
                if (_logChangedCommand == null)
                {
                    _logChangedCommand = new RelayCommand<TextChangedEventArgs>((e) => {
                        var textbox = e.OriginalSource  as TextBox;
                        Action scrollToEnd = () =>
                        {
                            if (textbox != null)
                                textbox.ScrollToEnd();
                        };
                        Application.Current.Dispatcher.Invoke(scrollToEnd);
                    });
                }
                return _logChangedCommand;
            }
        }
    }
}
